#include "Game.h"
#include "XMLParser.h"



using namespace std;




/**
 * Loads information from XML file. A structure with weapons/armor information linked with monster information
 * @param void
 * @return void
 */
void Game::loadItemsForMonsters()
{
	vector<s_ItemsForMonster*>	itemList;
	XMLParser appConfig(GAME_LOADCONFIG_ITEMSFORMONSTER_ROOTELEM, GAME_LOADCONFIG_ITEMSFORMONSTER_FIRSTELEM);
	
	appConfig.addATTROption(GAME_LOADCONFIG_ITEMSFORMONSTER_ATTR1);
	appConfig.addATTROption(GAME_LOADCONFIG_ITEMSFORMONSTER_ATTR2);
	appConfig.addATTROption(GAME_LOADCONFIG_ITEMSFORMONSTER_ATTR3);
	appConfig.readItemsForMonsters(GAME_LOADCONFIG_ITEMSFORMONSTER_FILENAME, itemList);
	extractItemsForMonstersFromStruct(itemList);
	freeItemsForMonstersStruct(itemList);
}


/**
 * Delete the temporary structure  
 * @param vector<s_ItemsForMonster*> itemList - items with monster description
 * @return void
 */
void Game::freeItemsForMonstersStruct(vector<s_ItemsForMonster*> itemList)
{
	for (unsigned int i = 0; i < itemList.size(); ++i)
	{
		delete itemList[i];
		itemList.erase(itemList.begin() + i);
	}
}


/**
 * For each Monster in the Game 
 * Create and Equip the weapon described in the XML config file for 
 * @param vector<s_ItemsForMonster*> itemList - items with monster description
 * @return void
 */
void Game::extractItemsForMonstersFromStruct(vector<s_ItemsForMonster*> itemList)
{
	for (unsigned int i = 0; i < m_monsters.size(); ++i)
	{
		for (unsigned int j = 0; j < itemList.size(); ++j)
		{
			if (!(m_monsters[i]->getName()).compare(itemList[j]->m_monster_name))
			{
				int itemId = 0;

				itemId = m_monsters[i]->addEquipment(itemList[j]->m_item_name, m_items);
				if (itemId != 0)
				{
					switch (itemList[j]->m_equiped)
					{
					case ITEM_EQUIP_BACKPACK:
						m_monsters[i]->addInBackpack(itemId);
						break;
					case ITEM_EQUIP_RIGHTHAND:
						m_monsters[i]->equipRightHand(itemId);
						break;
					case ITEM_EQUIP_LEFTHAND:
						m_monsters[i]->equipLeftHand(itemId);
						break;
					case ITEM_EQUIP_BODY:
						m_monsters[i]->equipBody(itemId);
						break;
					default:
						cout << "getItemsForMonstersFromStruct: Parse error. Check XML File.";
						break;
					}
				}
				else
				{
					cout << "getItemsForMonstersFromStruct: Parse error. Check XML File.";
				}
			}
		}
	}
}


/**
 * Prints out information about all the Items held by the Monsters of the Game
 * @param void
 * @return void
 */
void Game::showItemsForMonsters()
{
	for (unsigned int i = 0; i < m_monsters.size(); ++i)
	{
		cout << "Equipment for " << m_monsters[i]->getName() << endl;
		m_monsters[i]->printOnlyEquipment();
		m_monsters[i]->printOnlyBackpack();
		m_monsters[i]->printOnlyRightHand();
		m_monsters[i]->printOnlyLeftHand();
		m_monsters[i]->printOnlyBody();
	}
}
